package com.supercube.sport.yoga.service.impl;

import com.github.pagehelper.PageHelper;
import com.supercube.common.utils.ShiroUtils;
import com.supercube.sport.yoga.dao.CourseBasicInfoMapper;
import com.supercube.sport.yoga.dao.CourseLabelMapper;
import com.supercube.sport.yoga.dao.CoursesSupportedCardMapper;
import com.supercube.sport.yoga.dao.LabelMapper;
import com.supercube.sport.yoga.domain.CourseBasicInfoDTO;
import com.supercube.sport.yoga.domain.CourseLabelDTO;
import com.supercube.sport.yoga.domain.CoursesSupportedCardDTO;
import com.supercube.sport.yoga.domain.LabelDTO;
import com.supercube.sport.yoga.service.CourseBasicInfoService;
import com.supercube.sport.yoga.utils.UserUtils;
import com.supercube.sport.yoga.vo.CourseBasicInfoVO;
import com.supercube.sport.yoga.vo.CourseSuportCard;
import com.supercube.sport.yoga.vo.SuportCardVo;
import com.supercube.system.dao.UserDao;
import com.supercube.system.domain.UserDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Description：
 * Created with IntelliJ IDEA
 * Created By guowen
 * Date: 2020/3/21
 * Time: 17:13
 */
@Service
public class CourseBasicInfoServiceImpl implements CourseBasicInfoService {
    @Autowired
    private CourseBasicInfoMapper courseBasicInfoMapper;
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private CourseLabelMapper courseLabelMapper;

    @Autowired
    private UserDao userDao;
    @Autowired
    private CoursesSupportedCardMapper coursesSupportedCardMapper;

    public String save(CourseBasicInfoDTO dto) {
        Long userId = ShiroUtils.getUserId();
        UserUtils.setRecordBase(dto);
        dto.setId(uuid());
        //保存标签
        setLabel(dto);
        dto.setStatus("1");
        int insert = courseBasicInfoMapper.insert(dto);
        if (insert > 0) {
            return dto.getId();
        }
        return "";
    }

    private void setLabel(CourseBasicInfoDTO dto) {
        List<LabelDTO> labelList = dto.getLableList();
        if (CollectionUtils.isNotEmpty(labelList)) {
            labelList.forEach(label -> {
                label.setId(uuid());
                label.setLabelType("2");
                label.setStatus("1");
                UserUtils.setRecordBase(label);
                labelMapper.insert(label);

                CourseLabelDTO courseLabel = new CourseLabelDTO();
                courseLabel.setId(uuid());
                courseLabel.setCourseId(dto.getId());
                courseLabel.setLabelId(label.getId());
                courseLabelMapper.insert(courseLabel);
            });
        }
    }

    public Integer update(CourseBasicInfoDTO dto) {
        //删除原有的标签
        labelMapper.deleteByCourseId(dto.getId());
        courseLabelMapper.deleteByCourseId(dto.getId());
        //保存标签
        setLabel(dto);
        return courseBasicInfoMapper.updateByPrimaryKeySelective(dto);
    }

    public Integer deleteCourseBasic(String id) {
        //删除原有的标签
        labelMapper.deleteByCourseId(id);
        courseLabelMapper.deleteByCourseId(id);
        return courseBasicInfoMapper.deleteByPrimaryKey(id);
    }

    public CourseBasicInfoVO courseBasicInfo(String id) {
        CourseBasicInfoVO vo = courseBasicInfoMapper.selectByPrimaryKey(id);
        Long coachId = vo.getCoachId();
        if (coachId != null) {
            UserDO userDO = userDao.get(coachId);
            vo.setCoachName(userDO.getName());
            // ??? vo.setUser(userDO);
        }
        //查询标签
        List<LabelDTO> labelDTOList = labelMapper.selectByCourseId(id);
        vo.setLableList(labelDTOList);
        return vo;
    }

    @Override
    public List<CourseBasicInfoVO> selectAll(String classType) {
        return courseBasicInfoMapper.selectAll(classType);
    }

    public List<CourseBasicInfoVO> selectCourseBasicList(CourseBasicInfoDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<CourseBasicInfoVO> list = courseBasicInfoMapper.selectCourseBasicList(dto);
        List<LabelDTO> labelDTOS = labelMapper.selectAll();
        Map<String, List<LabelDTO>> labelDTOMap = labelDTOS.stream().collect(Collectors.groupingBy(LabelDTO::getCourseId));
        list.forEach(vo -> {
            List<LabelDTO> labelDTOList = labelDTOMap.get(vo.getId());
            vo.setLableList(labelDTOList);
        });
        return list;
    }

    public List<SuportCardVo> queryCoursesSupportedCard(String courseId) {
        List<SuportCardVo> list = coursesSupportedCardMapper.queryCoursesSupportedCard(courseId);
        return list;
    }

    public void saveCoursesSupportedCard(CourseSuportCard csc) {
        List<CoursesSupportedCardDTO> list = new ArrayList<>();
        List<SuportCardVo> cardCosts = csc.getCardCosts();
        String courseId = "";
        for (SuportCardVo cardCost : cardCosts) {
            CoursesSupportedCardDTO dto = new CoursesSupportedCardDTO();
            dto.setId(uuid());
            dto.setCardId(cardCost.getCardId());
            dto.setCourseId(cardCost.getCourseId());
            courseId = cardCost.getCourseId();
            UserUtils.setRecordBase(dto);
            if (Objects.equals(cardCost.getCardType(), "2")) {
                //次卡
                dto.setTimesPeopleGroup(cardCost.getTimesPeopleGroup());
                dto.setTimesPeople1to1(cardCost.getTimesPeople1to1());
                dto.setTimesPeople1to2(cardCost.getTimesPeople1to2());
                dto.setTimesPeople1to3(cardCost.getTimesPeople1to3());
            } else {
                //储值类型、期限类型
                dto.setCostPerClassPersonGroup(cardCost.getCostPerClassPersonGroup());
                dto.setCostPerClassPerson1to1(cardCost.getCostPerClassPerson1to1());
                dto.setCostPerClassPerson1to2(cardCost.getCostPerClassPerson1to2());
                dto.setCostPerClassPerson1to3(cardCost.getCostPerClassPerson1to3());
            }
            list.add(dto);
        }
        if (CollectionUtils.isNotEmpty(list)) {
            //删除原有的课程关联的会员卡
            coursesSupportedCardMapper.deleteCoursesSupportedCardByCourseId(courseId);
            coursesSupportedCardMapper.batchInsert(list);
        }

    }

    private String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}
